#include "ui_start_menu.h"

#include <QFontDatabase>

UiStartMenu::UiStartMenu(const QRectF &start_menu_pos, const int& system_style,QGraphicsItem *parent)
    : QGraphicsItem{parent}, start_menu_pos_(start_menu_pos), system_style_(system_style)
{
    setProperty("StyleSheet","StartMenu");

    start_menu_image_.load(":/img_res/fake_desktop_icon/start_menu.svg");
    QImage img = start_menu_image_.scaledToWidth(16,Qt::SmoothTransformation);
    start_menu_image_ = img;

    magnifier_image_.load(":/img_res/fake_desktop_icon/magnifier.svg");
    img = magnifier_image_.scaledToWidth(16,Qt::SmoothTransformation);
    magnifier_image_ = img;

    if(system_style_ == 0)
    {
        //深色
        start_menu_image_.invertPixels();
    }

    QString time_str = QDateTime::currentDateTime().toString("hh:mm");
    QString date_str = QDateTime::currentDateTime().toString("yyyy/M/dd");
    date_time_str_ = time_str + QChar('\n') + date_str;
    update();
    startTimer(10000);
}

void UiStartMenu::timerEvent(QTimerEvent *evt)
{
    QString time_str = QDateTime::currentDateTime().toString("hh:mm");
    QString date_str = QDateTime::currentDateTime().toString("yyyy/M/dd");
    date_time_str_ = time_str + QChar('\n') + date_str;
    update();
}

QRectF UiStartMenu::boundingRect() const
{
    return start_menu_pos_;
}

void UiStartMenu::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);

    QPen pen(Qt::NoPen);
    painter->setPen(pen);
    painter->save();

    if(system_style_ == 0)
    {
        //深色
        painter->setBrush(dark_color_);
    }
    else
    {
        painter->setBrush(light_color_);
    }

    painter->drawRect(start_menu_pos_);
    painter->restore();
    painter->save();

    //开始菜单图标
    painter->drawPixmap(QRect(start_menu_pos_.topLeft().toPoint() + QPoint(16,10),\
                              start_menu_image_.size()),\
                        QPixmap::fromImage(start_menu_image_));
    //搜索框（仅开始菜单在下方时展示）
    QRectF search_box_rect(QPointF(start_menu_pos_.topLeft().toPoint() + QPointF(48,0)),\
                           QSizeF(350,start_menu_pos_.height()));
    painter->setBrush(QColor(255,255,255));
    QPen border_pen;
    border_pen.setStyle(Qt::SolidLine);
    border_pen.setColor(QColor(0,0,0));
    border_pen.setWidthF(0.2);
    painter->setPen(border_pen);
    painter->drawRect(search_box_rect);
    painter->restore();
    painter->save();
    painter->drawPixmap(QRect(search_box_rect.topLeft().toPoint() + QPoint(15,10),\
                              magnifier_image_.size()),\
                        QPixmap::fromImage(magnifier_image_));

    QFont font(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
    font.setPixelSize(13);
    painter->setPen(QColor(0,0,0));
    painter->setFont(font);
    painter->drawText(search_box_rect.topLeft().toPoint() + QPoint(40,24),QString("在这里输入你要搜索的内容"));

    //右侧时间框
    if(system_style_ == 0)
    {
        //深色
        painter->setPen(QColor(255,255,255));
    }
    else
    {
        painter->setPen(QColor(0,0,0));
    }
    painter->drawText(QRectF(start_menu_pos_.topRight() - QPointF(130,0),QSizeF(80,start_menu_pos_.height())),Qt::AlignCenter,date_time_str_);
    painter->restore();
    painter->save();

    //通知中心
    QPointF offset_point = start_menu_pos_.topRight() - QPointF(37,-13);
    double info_center_width = 15.0;
    double info_center_height = 12.0;
    double delta = 5.0;
    static const QPointF points[7] = {
        QPointF(0.0, 0.0)                                         + offset_point,
        QPointF(info_center_width, 0.0)                           + offset_point,
        QPointF(info_center_width, info_center_height)            + offset_point,
        QPointF(info_center_width - delta, info_center_height)    + offset_point,
        QPointF(info_center_width / 2, info_center_height + 3.0)+ offset_point,
        QPointF(delta, info_center_height)                        + offset_point,
        QPointF(0.0, info_center_height)                          + offset_point
    };

    if(system_style_ == 0)
    {
        //深色
        painter->setPen(QColor(255,255,255));
    }
    else
    {
        painter->setPen(QColor(0,0,0));
    }
    painter->setBrush(Qt::NoBrush);
    painter->drawPolygon(points, 7);
    painter->restore();
    painter->save();
    //显示桌面的线
    if(system_style_ == 0)
    {
        //深色
        painter->setPen(QColor(255,255,255));
    }
    else
    {
        painter->setPen(QColor(150,150,150));
    }
    painter->drawLine(start_menu_pos_.topRight() - QPointF(5,0),start_menu_pos_.bottomRight() - QPointF(5,0));
    painter->restore();
    painter->save();
    //输入法
    font.setBold(true);
    font.setPixelSize(16);
    painter->setFont(font);
    if(system_style_ == 0)
    {
        //深色
        painter->setPen(QColor(255,255,255));
    }
    else
    {
        painter->setPen(QColor(0,0,0));
    }
    painter->drawText(start_menu_pos_.topRight() - QPointF(160,-25),"中");
    painter->restore();
}
